<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<?php
// check if the user is logged-in
include 'authentication.php';
isAuthorized();

include_once("../utils/redirect.php");
include_once($_SERVER["DOCUMENT_ROOT"]."/web-doc/protected/database/MySqlDAO.php");

if (!empty($_GET['idc'])) {
    $idc = $_GET['idc'];
    if (!empty($_POST['isToInsert'])) {
        if (!empty($_POST['title']) && !empty($_POST['url'])) {
            $title = $_POST['title'];
            $url = $_POST['url'];

            $mySqlDAO = new MySqlDAO();
            
             /* Check if title is already used */
            $idlres = $mySqlDAO->checkLinkTitle($idc, $title);
            if(!empty($idlres)) {
                echo "Il nome <b>$title</b> è già usato per un'altro link. Scegliere un'altro nome.<br>";
            }
            else {
                $mySqlDAO->createLink($title, $url, $idc);
                redirect("showcategory.php?idc=$idc",FALSE);
            }
            
        } else {
            echo "Inserire tutti i dati.";
        }
    } else {
        $title = "";
        $url = "";
    }        
} else {
    die("ERRORE: Parent directory mancante. <a href=javascript:history.back(-1)>Indietro</a>");
}
?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../styles/main.css">
        <link rel="stylesheet" type="text/css" href="../styles/admin.css">
        <title>WebDoc - Create link</title>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data" action="createlink.php?idc=<?php echo $idc ?>">
            <table width="450" border="0" cellpadding="1" cellspacing="1" class="box">
                <tr>
                    <td valign="top" colspan="2">
                        Nome link: <input type="text" name="title" size="50" maxlength="255" value="<?php echo $title ?>">
                        <input type="hidden" name="isToInsert" value="true">
                    </td>
                </tr>
                <tr>
                    <td valign="top" colspan="2">
                        URL: <input type="text" name="url" size="50" maxlength="255" value="http://">
                    </td>
                </tr>
                <tr>
                    <td width="80" colspan="2">
                        <br><br>
                        <input type="submit" value="Crea" />
                        <!--<input name="upload" type="submit" class="box" id="upload" value=" Upload ">-->
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

